package com.wentao.mall.config;

import com.wentao.mall.commons.Constant;
import com.wentao.mall.filter.AdminFilter;
import com.wentao.mall.filter.CustomerFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyConfig {
//        @Bean
////    public WebMvcConfigurer webMvcConfigurer(){
////        return new WebMvcConfigurer(){
////            @Override
////            public void addCorsMappings(CorsRegistry registry) {
////                //为当前工程的所有路径配置跨域问题
//                registry.addMapping("/**")
//                        //允许所有来源跨域
//                        .allowedOrigins("*")
//                        //允许所有请求头跨域
//                        .allowedHeaders("*")
//                        //允许所有请求方式跨域
//                        .allowedMethods("*");
//            }
//        };
//    }

    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                //TODO
                registry.addResourceHandler("/images/*")
                        .addResourceLocations("file:"+ Constant.FILE_PATH);
            }
        };
    }

//    @Bean
//    public CorsFilter corsFilter() {
//        return new CorsFilter();
//    }
//
//    @Bean
//    public FilterRegistrationBean corsFilterRegistrationBean(@Autowired CorsFilter corsFilter) {
//        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(corsFilter);
//        filterRegistrationBean.addUrlPatterns("/*");
//        filterRegistrationBean.addUrlPatterns("/*/*");
//        filterRegistrationBean.addUrlPatterns("/*/*/*");
//        return filterRegistrationBean;
//    }

    @Bean
    public AdminFilter adminFilter() {
        return new AdminFilter();
    }

    @Bean
    public FilterRegistrationBean AdminfilterRegistrationBean(@Autowired AdminFilter adminFilter) {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(adminFilter);
        //管理员操作的接口都进行过滤，要求登录且有管理员权限
        filterRegistrationBean.addUrlPatterns("/admin/category/*");
        filterRegistrationBean.addUrlPatterns("/admin/product/*");
        filterRegistrationBean.addUrlPatterns("/admin/order/*");
        return filterRegistrationBean;
    }

    @Bean
    public CustomerFilter customerFilter() {
        return new CustomerFilter();
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean(@Autowired CustomerFilter customerFilter){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(customerFilter);
        filterRegistrationBean.addUrlPatterns("/cart/*");
        filterRegistrationBean.addUrlPatterns("/order/*");
        return filterRegistrationBean;
    }
}